package com.wk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wk.domain.FcBuilding;
import com.wk.domain.FcCell;
import com.wk.domain.FcEstate;
import com.wk.domain.FcUnit;
import com.wk.mapper.FcEstateMapper;
import com.wk.service.FcBuildingService;
import com.wk.service.FcCellService;
import com.wk.service.FcEstateService;
import com.wk.service.FcUnitService;
import com.wk.vo.CellMessage;
import com.wk.vo.UnitMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FcEstateServiceImpl extends ServiceImpl<FcEstateMapper, FcEstate> implements FcEstateService {

	@Autowired
	private FcBuildingService buildingService;

	@Autowired
	private FcUnitService unitService;

	@Autowired
	private FcCellService cellService;

	@Override
	public Integer insertEstate(FcEstate fcEstate) {
		//判断住宅编码是否存在，不存在才允许插入
		LambdaQueryWrapper<FcEstate> wrapper = new LambdaQueryWrapper<FcEstate>().eq(FcEstate::getEstateCode,fcEstate.getEstateCode());
		FcEstate estate = baseMapper.selectOne(wrapper);
		if (estate == null) {
			return baseMapper.insert(fcEstate);
		}else{
			return 0;
		}
	}

	@Override
	public List<FcBuilding> selectBuilding(Integer buildingNumber, String estateCode) {
		List<FcBuilding> list = new ArrayList<>();
		for (int i = 0; i < buildingNumber; i++) {
			FcBuilding building = new FcBuilding();
			building.setEstateCode(estateCode);
			building.setBuildingCode(estateCode+"B"+(i+1));
			building.setBuildingName("第"+(i+1)+"号楼");
			list.add(building);
		}
		buildingService.saveBatch(list);
		return list;
	}

	@Override
	public Boolean updateBuildingById(FcBuilding building) {
		return buildingService.updateById(building);
	}

	@Override
	public List<FcUnit> selectUnit(UnitMessage unitMessage) {
		List<FcUnit> list = new ArrayList<>();
		for (int i = 0; i < unitMessage.getUnitCount(); i++) {
			FcUnit unit = new FcUnit();
			unit.setBuildingCode(unitMessage.getBuildingCode());
			unit.setUnitCode(unitMessage.getBuildingCode()+"U"+(i+1));
			unit.setUnitName("第"+(i+1)+"单元");
			list.add(unit);
		}
		unitService.saveBatch(list);
		return list;
	}

	@Override
	public List<FcCell> insertCell(CellMessage[] cellMessages) {
		List<FcCell> list = new ArrayList<>();
		//遍历楼层
		for (CellMessage cellMessage : cellMessages) {
			for (int i = 1; i <= cellMessage.getStopFloor(); i++) {
				//房间号
				for (int j = cellMessage.getStartCellId(); j <= cellMessage.getStopCellId(); j++) {
					FcCell cell = new FcCell();
					cell.setUnitCode(cellMessage.getUnitCode());
					cell.setCellName(i+"0"+j);
					cell.setCellCode(cellMessage.getUnitCode()+"C"+i+"0"+j);
					cell.setFloorNumber(i);
					list.add(cell);
				}
			}
		}
		cellService.saveBatch(list);
		return list;
	}

	@Override
	public List<FcBuilding> selectBuildingByEstate(String estateCode) {
		LambdaQueryWrapper<FcBuilding> wrapper = new LambdaQueryWrapper<FcBuilding>()
				.eq(FcBuilding::getEstateCode,estateCode)
				.select(FcBuilding::getBuildingName,FcBuilding::getBuildingCode);

		return buildingService.list(wrapper);
	}

	@Override
	public List<FcUnit> selectUnitByBuildingCode(String buildingCode) {
		LambdaQueryWrapper<FcUnit> wrapper = new LambdaQueryWrapper<FcUnit>()
				.eq(FcUnit::getBuildingCode,buildingCode)
				.select(FcUnit::getUnitCode,FcUnit::getUnitName);
		return unitService.list(wrapper);
	}

	@Override
	public List<FcCell> selectCellByUnitCode(String unitCode) {
		LambdaQueryWrapper<FcCell> wrapper = new LambdaQueryWrapper<FcCell>()
				.eq(FcCell::getUnitCode,unitCode);
		return cellService.list(wrapper);
	}

	@Override
	public List<FcEstate> selectEstate(String companyId) {
		LambdaQueryWrapper<FcEstate> wrapper = new LambdaQueryWrapper<FcEstate>()
				.eq(FcEstate::getCompany,companyId)
				.select(FcEstate::getEstateName,FcEstate::getEstateCode);

		return baseMapper.selectList(wrapper);
	}
}
